<template>
  <div>
    <h2>用户管理</h2>
    <el-table :data="users" style="width: 100%">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="name" label="姓名" />
      <el-table-column prop="role" label="角色" />
      <el-table-column prop="email" label="邮箱" />
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button size="mini" @click="editUser(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteUser(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 编辑用户弹窗 -->
    <el-dialog title="编辑用户" v-model="dialogVisible" width="400px">
      <el-form :model="editForm" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="editForm.name" />
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="editForm.role">
            <el-option label="学生" value="学生" />
            <el-option label="教师" value="教师" />
            <el-option label="管理员" value="管理员" />
          </el-select>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="editForm.email" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveUser">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'

const users = ref([
  { id: 1, name: '张三', role: '学生', email: 'zhangsan@example.com' },
  { id: 2, name: '李四', role: '教师', email: 'lisi@example.com' },
  { id: 3, name: '王五', role: '管理员', email: 'wangwu@example.com' }
])

const dialogVisible = ref(false)
const editForm = ref({ id: '', name: '', role: '', email: '' })

function editUser(row) {
  editForm.value = { ...row }
  dialogVisible.value = true
}
function saveUser() {
  // TODO: 调用后端接口保存
  ElMessage.success('保存成功')
  dialogVisible.value = false
}
function deleteUser(row) {
  // TODO: 调用后端接口删除
  users.value = users.value.filter(u => u.id !== row.id)
  ElMessage.success('删除成功')
}
</script>